package com.example.tellwin.view;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cncoderx.wheelview.Wheel3DView;
import com.example.tellwin.R;
import com.example.tellwin.base.BaseDialog;
import com.example.tellwin.mine.bean.CityBean;
import com.example.tellwin.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/13
 * @Describe ：
 **/
public class AddressDialog extends BaseDialog {

    private TextView cancelTv;
    private TextView titleTv;
    private TextView confrimBtn;
    private Wheel3DView provinceWheelView;
    private Wheel3DView cityWheelView;
    private Wheel3DView areaWheelView;

    List<CityBean> cityBeans = new ArrayList<>();
    private int provincePosition;
    private int cityPosition;
    private int areaPosition;

    private OnAddressSelectListener onSelectListener;

    public void setOnSelectListener(OnAddressSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public AddressDialog(@NonNull Context context) {
        super(context , R.style.ActionSheetDialogStyle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_address);
        Window window = this.getWindow();
        //设置位置在屏幕底部
        window.setGravity(Gravity.BOTTOM);
        //设置弹入弹出动画
        window.setWindowAnimations(R.style.BottomDialog_Animation);
        //设置为全屏dialog
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        cityBeans = CommonUtils.cityBeans("city.json", getContext());

        initView();
        initEvent();
        initData();
    }

    private void initView() {
        cancelTv = (TextView) findViewById(R.id.cancel_tv);
        titleTv = (TextView) findViewById(R.id.title_tv);
        confrimBtn = (TextView) findViewById(R.id.confrim_btn);
        provinceWheelView = findViewById(R.id.province_wheel_view);
        cityWheelView = findViewById(R.id.city_wheel_view);
        areaWheelView = findViewById(R.id.area_wheel_view);

    }

    private void initEvent() {
        cancelTv.setOnClickListener(v -> {
            dismiss();
        });
        confrimBtn.setOnClickListener(v -> {
            if (onSelectListener != null) {
                onSelectListener.onAddressSelect(cityBeans.get(provincePosition).getAreaName(),
                        cityBeans.get(provincePosition).getCities().get(cityPosition).getAreaName(),
                        cityBeans.get(provincePosition).getCities().get(cityPosition).getCounties().get(areaPosition).getAreaName(),
                        provincePosition, cityPosition, areaPosition);
            }
            dismiss();
        });
        provinceWheelView.setOnWheelChangedListener((view, oldIndex, newIndex) -> {
            provincePosition = newIndex;
            cityWheelView.setEntries(cityBeans.get(newIndex).getCities());
            areaWheelView.setEntries(cityBeans.get(newIndex).getCities().get(0).getCounties());
        });
        cityWheelView.setOnWheelChangedListener((view, oldIndex, newIndex) -> {
            cityPosition = newIndex;
            areaWheelView.setEntries(cityBeans.get(provincePosition).getCities().get(newIndex).getCounties());
        });
        areaWheelView.setOnWheelChangedListener((view, oldIndex, newIndex) -> areaPosition = newIndex);
    }

    private void initData() {
        provinceWheelView.setEntries(cityBeans);
        cityWheelView.setEntries(cityBeans.get(0).getCities());
        areaWheelView.setEntries(cityBeans.get(0).getCities().get(0).getCounties());
    }

    public interface OnAddressSelectListener {
        void onAddressSelect(String province, String city, String area, int provincePosition, int cityPosition, int areaPosition);
    }

}
